﻿using Book.Entities;
using Book.IRepositories;
using Book.JWT;
using Book.Models.UserWai;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Book.Controllers
{
    [Route("api/user")]
    [ApiController]
    [EnableCors("any")]
    public class UserController : ControllerBase
    {
        private readonly IUserRepository _userRepositiry;

        public UserController(IUserRepository userRepositiry)
        {
            this._userRepositiry = userRepositiry;
        }
        /// <summary>
        /// 获得所有用户
        /// </summary>
        /// <returns></returns>
        [HttpGet("all")]
        public IActionResult GetAllUser()
        {
            var rel = _userRepositiry.GetAllUser();
            return Ok(rel);
        }
        [HttpPost("login")]
        public IActionResult Login([FromBody] JObject parameter)
        {
            //var dd = new { Id = 1, Name = 3 };
            //List<dynamic> collection = new List<dynamic>();
            //for (int i=0;i<10;i++)
            //{
            //    var d = new { Id = 1, name = 2 };
            //    collection.Add(new { Id = 11, name = 22, children = d });
            //}
            string username = parameter.Value<string>("username");
            string password = parameter.Value<string>("password");
            var rel=_userRepositiry.Login(username, password);         
            return Ok(rel);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userWai">外模式的user</param>
        /// <returns></returns>
        [HttpPost("add")]
        public IActionResult AddUser(UserWai userWai)
        {
            _userRepositiry.AddUser(userWai);
            return Ok();
        }
        [HttpDelete("delete")]
        public IActionResult DeleteUser(string id)
        {
            _userRepositiry.DeleteUser(id);
            return Ok();
        }
        [HttpPut("update")]
        public IActionResult UpdateUser(UserEntity userEntity)
        {
            _userRepositiry.UpdateUser(userEntity);
            return Ok();
        }
    }
    enum Color
    {
        Red,
        Green,
        Blue
    }
    class EnumTypeExample
    {
        static void PrintColor(Color color)
        {
            switch (color)
            {
                case Color.Red:
                    Console.WriteLine("Red");
                    break;
                case Color.Green:
                    Console.WriteLine("Green");
                    break;
                case Color.Blue:
                    Console.WriteLine("Blue");
                    break;
                default:
                    Console.WriteLine("Unknown color");
                    break;
            }

        }
    }
}
